package io.jchat.android.application;
import android.content.ContentResolver;
import android.content.Intent;
import android.app.Activity;
import android.graphics.Color;
import android.widget.Toast;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.PluginResult;
import android.util.Log;
import android.provider.Settings;
import android.os.Bundle;
import android.widget.Toast;
import org.json.JSONArray;

import org.json.JSONException;
import org.json.JSONObject;
import io.jchat.android.controller.LoginController;
import io.jchat.android.activity.MainActivity;
public class GDJMessage extends CordovaPlugin {

	public static final String TAG = "GDJMessage Plugin";
	private CallbackContext callbackContext;
	private static String action_login = "login";
	private static String action_regist = "regist";
	private static String action_main = "getAllConversationList";
	private static String action_singleConv = "creatConversation";
	private static String action_logout = "logout";
    private LoginController lc;
    final int duration = Toast.LENGTH_SHORT;
/**
* Constructor.
*/
    public GDJMessage() {}
/**
							 * Sets the context of the Command. This can then be
							 * used to do things like get file paths associated
							 * with the Activity.
							 *
							 * @param cordova
							 *            The context of the main Activity.
							 * @param webView
							 *            The CordovaWebView Cordova is running
							 *            in.
							 */
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
		super.initialize(cordova, webView);
		Log.v(TAG, "Init CoolPlugin");
	}

public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {


// Shows a toast
Log.v(TAG,"chenyue:GDJMessage received:"+ action);

if(action_login.equals(action)){
    return  	login(args.getString(0),args.getString(1));

}else if(action_regist.equals(action)){
	return      regist(args.getString(0),args.getString(1));      
}else if(action_main.equals(action)){
	return      play(MainActivity.class,args.getString(0),action);
	
}else if(action_singleConv.equals(action)){
	Log.v(TAG,"chenyue:GDJMessage nothing to open!");
	return false;
}
else if(action_logout.equals(action)){
	Log.v(TAG,"chenyue:GDJMessage nothing to open!");
	return false;
}else{
	Log.v(TAG,"chenyue:GDJMessage nothing to open!");
	return false;
}
     
};
public boolean  login(String name ,String pwd){
    lc=new LoginController();
    boolean state=	lc.goMain(name,pwd,null);
    if(state==true){
    	callbackContext.success();
    }else{
    	this.callbackContext.error("login is failed");
    }
return state;
	
};
public boolean  regist(String name ,String pwd){
	lc=new LoginController();
    boolean state=	lc. goRegist(name,pwd);
    if(state==true){
	     callbackContext.success();
     }else{
	           this.callbackContext.error("regist is failed");
     }
return state;
	
};
public boolean play(final Class activityClass,final String argsA,final String argsB){
	final CordovaInterface cordovaObj = cordova;
	final CordovaPlugin plugin = this;
cordova.
getActivity().
runOnUiThread(
		new Runnable()
		{
         public void run() 
         {
        	 final Intent streamIntent = new Intent(cordovaObj.getActivity().getApplicationContext(), activityClass)
        			 ;
        	        Bundle extras = new Bundle()
        	        ;
        	        
        	    	extras.putString("param0", argsA)
        	    	;
        	    	extras.putString("param1", argsB)
        	    	;
                    Toast toast = Toast.makeText(cordova.getActivity().getApplicationContext(), argsB, duration)
                    ;
                     toast.show()
                     ;
                 	streamIntent.putExtras(extras)
                 	;  
                	cordovaObj.startActivityForResult(plugin, streamIntent,7)
                	;
}
}
)
;
return true
		;
}

}
